<?php

header("Content-type:application/json; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

require('../service/user_service.php');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$wsname = $_REQUEST['wsname'];
$tokeyKey = $_REQUEST['token'];

class user_ws
{

    public function getAlluser()
    {
        $user_service = new user_service();
        $lists = $user_service->findAll();
        return apputil::getJsonDataResultUtil($lists, 'users');
    }

    public function getUserById($id)
    {
        $user_service = new user_service();
        $lists = $user_service->findUserById($id);
        return apputil::getJsonDataResultUtil($lists, 'users');
    }

    public function createUser($userObject)
    {
        $user_service = new user_service();
        $flag = $user_service->create($userObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function updateUser($userObject)
    {
        $user_service = new user_service();
        $flag = $user_service->update($userObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function deleteUser($userObject)
    {
        $user_service = new user_service();
        $flag = $user_service->delete($userObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function isCheckDuplicateUser($userObject)
    {
        $user_service = new user_service();
        $flag = $user_service->isDuplicationUser($userObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function isValidateToken($tokeyKey)
    {
        $user_service = new user_service();
        return $user_service->isValidateTokenKey($tokeyKey);
    }

}

//init class
$user_ws = new user_ws();

if ($user_ws->isValidateToken($tokeyKey)) {
    if ($wsname == 'getallusers') {
        print_r($user_ws->getAlluser());
    } else if ($wsname == 'getuserbyid') {
        print_r($user_ws->getUserById($_REQUEST['id']));
    } else if ($wsname == 'createuser' || $wsname == 'updateuser') {
        $user = new user();

        $user->setUsername(isset($_REQUEST['username']) ? $_REQUEST['username'] : null);
        $user->setPassword(isset($_REQUEST['password']) ? $_REQUEST['password'] : null);
        $user->setDisplayname(isset($_REQUEST['displayname']) ? $_REQUEST['displayname'] : null);
        $user->setEmail(isset($_REQUEST['email']) ? $_REQUEST['email'] : null);
        $user->setFirstname(isset($_REQUEST['firstname']) ? $_REQUEST['firstname'] : null);
        $user->setLastname(isset($_REQUEST['lastname']) ? $_REQUEST['lastname'] : null);
        $user->setAge(isset($_REQUEST['age']) ? $_REQUEST['age'] : null);
        $user->setBirthday(isset($_REQUEST['birthday']) ? $_REQUEST['birthday'] : null);
        $user->setUsertype(isset($_REQUEST['usertype']) ? $_REQUEST['usertype'] : null);
        $user->setStatus(isset($_REQUEST['status']) ? $_REQUEST['status'] : null);

        if ($wsname == 'createuser') {
            print_r($user_ws->createUser($user));
        } else if ($wsname == 'updateuser') {
            $user->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
            print_r($user_ws->updateuser($user));
        }

    } else if ($wsname == 'deleteuser') {
        $user = new user();
        $user->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
        print_r($user_ws->deleteUser($user));
    } else if ($wsname == 'checkduplicateuser') {
        $user = new user();
        $user->setUsername(isset($_REQUEST['username']) ? $_REQUEST['username'] : null);
        $user->setEmail(isset($_REQUEST['email']) ? $_REQUEST['email'] : null);
        print_r($user_ws->isCheckDuplicateUser($user));
    } else {
        echo 'web service error !!';
    }
} else {
    echo 'Your token is incorrect !!';
}
?>